package com.thingsgrid.basicInterface.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.thingsgrid.basicInterface.entity.ApiInterfaceTypeRelation;
import com.thingsgrid.basicInterface.entity.BasicInterface;
import com.thingsgrid.basicInterface.entity.BasicInterfaceType;
import com.thingsgrid.basicInterface.mapper.ApiInterfaceTypeRelationMapper;
import com.thingsgrid.basicInterface.service.IApiInterfaceTypeRelationService;
import com.thingsgrid.basicInterface.service.IBasicInterfaceService;
import com.thingsgrid.basicInterface.service.IBasicInterfaceTypeService;
import com.thingsgrid.basicInterface.vo.BasicInterfaceVo;
import com.thingsgrid.basicInterface.vo.TypeTree;
import com.thingsgrid.basicInterface.wrapper.BasicInterfaceWrapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@ApiIgnore()
@RestController
@AllArgsConstructor
public class BasicInterfaceClientImpl implements  BasicInterfaceClient {


	private IBasicInterfaceService interfaceService;

	private IApiInterfaceTypeRelationService apiInterfaceTypeRelationService;

	private IBasicInterfaceTypeService basicInterfaceTypeService;

	public static  BasicInterfaceWrapper build = BasicInterfaceWrapper.build();


	@Override
	@GetMapping(APPAPI)
	public List<BasicInterfaceVo> getApplicationApi(@RequestParam(value = "apiIds") Collection<Long> apiIds) {
		Collection<BasicInterface> basicInterfaces = interfaceService.listByIds(apiIds);
		List<BasicInterfaceVo> basicInterfaceVos = BasicInterfaceWrapper.build().listVO(new ArrayList<>(basicInterfaces));
		return basicInterfaceVos;
	}


	@Override
	public List<BasicInterfaceVo> getTypeInterface(@RequestParam(value = "id") Long id) {
		List<BasicInterface> lists = interfaceService.list(new LambdaQueryWrapper<BasicInterface>()
			.eq(BasicInterface::getTypeId, id));
		List<BasicInterfaceVo> collect = lists.stream().map(e -> build.entityVO(e)).collect(Collectors.toList());
		return collect;
	}

	@Override
	@GetMapping(GETAPIBYGROP)
	public String getAPITree(@RequestParam(value = "apiIds") Collection<Long> apiIds) {
		return apiInterfaceTypeRelationService.getTypeTreesForAPP(new ArrayList<>(apiIds));
	}

	@Override
	@GetMapping(GETAPIBYPACKAGE)
	public String getAPITreeByPackage(@RequestParam(value = "apiIds") Collection<Long> apiIds) {
		HashSet<Long> typeIds = new HashSet<>();
		List<ApiInterfaceTypeRelation> list = apiInterfaceTypeRelationService.list(new LambdaQueryWrapper<ApiInterfaceTypeRelation>()
			.in(!apiIds.isEmpty(), ApiInterfaceTypeRelation::getApiId, apiIds));
		list.forEach( e -> typeIds.add(e.getTypeId()));
		List<BasicInterfaceType> types = basicInterfaceTypeService.list(new LambdaQueryWrapper<BasicInterfaceType>().eq(BasicInterfaceType::getCategory, "1"));
		List<TypeTree> typeTrees = types.stream().map(e -> new TypeTree(e,typeIds)).collect(Collectors.toList());

		return new Gson().toJson(typeTrees);
	}

}
